<template>
  <view>
    <view class="container">
      <uni-forms ref="form" :modelValue="form" labelWidth="120px" labelPosition="top">
        <view class="hidden-title">培训教育</view>
        <view class="uni-container">
          <uni-table ref="table" border stripe emptyText="暂无更多数据" @selection-change="selectionChange">
            <uni-tr>
              <uni-th width="50" align="center">序号</uni-th>
              <uni-th width="150" align="center">培训教育</uni-th>
            </uni-tr>
            <uni-tr v-for="(item, index) in form.deviceExamineTrainReceiveEntityList" :key="index">
              <uni-td>{{ index + 1 }}</uni-td>
              <uni-td>{{ item.trainMatter }}</uni-td>
            </uni-tr>
          </uni-table>
        </view>
        <view class="background-white">
          <uni-forms-item name="opinionType" label="验收意见" class="acceptance" required>
            <uni-data-checkbox
              v-model="form.opinionType"
              :localdata="opinions"
              @change="radioChange"
            ></uni-data-checkbox>
          </uni-forms-item>
        </view>
        <view class="background-white bor-top" v-if="form.opinionType === 'REPULSE'">
          <uni-forms-item name="targetNodeId" label="选择回退节点">
            <cu-cell
              title="回退节点"
              required
              link
              placeholder="请选择"
              :description="taskNode"
              @click="rollbackClick()"
            ></cu-cell>
          </uni-forms-item>
        </view>
        <view class="background-white bor-top">
          <uni-forms-item name="opinion" label="审查意见说明" required>
            <uni-easyinput
              type="textarea"
              v-model="form.opinion"
              placeholder="简要说明审批意见"
              maxlength="50"
            ></uni-easyinput>
          </uni-forms-item>
        </view>
      </uni-forms>
      <CuSelect
        v-if="form.opinionType === 'REPULSE'"
        ref="rollbackDict"
        title="选择回退节点"
        :options="backNodes"
        @update:value="rollbackConfirm"
      ></CuSelect>
    </view>
    <view class="submit-group">
      <button class="u-button" :ripple="true" @click="submit">提交</button>
    </view>
  </view>
</template>

<script>
import { trainReceive, getNode, getTrain } from '@/api/repair/repair.js'
import { mapGetters } from 'vuex'
import CuSelect from '@/components/CuSelect/index.vue'
import { getInfo } from '../../../../../../api/login'

export default {
  components: {
    CuSelect
  },
  data() {
    return {
      opinions: [
        { text: '合格', value: 'PASS' },
        { text: '不合格', value: 'REPULSE' }
      ],
      backNodes: [],
      id: '',
      procTaskId: '',
      form: {
        deviceExamineTrainReceiveEntityList: [],
        opinionType: '',
        opinion: '',
        targetNodeId: ''
      },
      taskNode: '',
      targetNodeTypeValue: '',
      rules: {
        opinionType: {
          rules: [
            {
              required: true,
              errorMessage: '验收意见不能为空'
            }
          ]
        },
        targetNodeId: {
          rules: [
            {
              required: true,
              errorMessage: '回退节点不能为空'
            }
          ]
        },
        opinion: {
          rules: [
            {
              required: true,
              errorMessage: '审查意见说明不能为空'
            }
          ]
        }
      }
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  onReady() {
    this.$refs.form.setRules(this.rules)
  },
  onLoad(option) {
    if (!option.data) return
    let data = JSON.parse(option.data)
    this.id = data.id
    this.procTaskId = data.procTaskId
    this.getTrainItem(this.id)
  },
  methods: {
    async getTrainItem(examineId) {
      const { user } = await getInfo()
      const res = await getTrain(examineId)
      res.data.map((item) => {
        this.form.deviceExamineTrainReceiveEntityList.push({
          trainMatter: item.trainMatter,
          trainId: item.id,
          trainer: user.name,
          trainReceiveId: this.userId
        })
      })
    },
    radioChange({ detail }) {
      if (detail.value === 'REPULSE') this.getBackNode(this.procTaskId)
    },
    getBackNode(procTaskId) {
      getNode(procTaskId).then((res) => {
        this.backNodes = res.data
        this.backNodes.map((m) => {
          m.label = m.nodeName
          m.value = m.nodeId
        })
      })
    },
    rollbackClick() {
      this.$refs.rollbackDict.open()
    },
    rollbackConfirm(obj) {
      this.taskNode = obj.name
      this.form.targetNodeId = obj.value
      let match = this.backNodes.find((n) => {
        return n.value === obj.value
      })
      this.targetNodeTypeValue = match.nodeTypeValue
    },
    submit() {
      this.$refs.form.validate().then((res) => {
        let data = {
          id: this.id,
          procTaskId: this.procTaskId,
          targetNodeId: this.form.targetNodeId,
          targetNodeTypeValue: this.targetNodeTypeValue,
          deviceExamineWorkflowEntity: {
            examineId: this.id,
            opinionType: this.form.opinionType,
            opinion: this.form.opinion
          },
          deviceExamineTrainReceiveEntityList: this.form.deviceExamineTrainReceiveEntityList
        }
        trainReceive(data).then((res) => {
          this.$modal.showToast('处理成功')
          uni.$emit('refresh')
          uni.navigateBack()
        })
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  margin-bottom: 200rpx;

  .background-white {
    background-color: #fff;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .uni-forms-item__label {
      color: #333;
      font-size: 30rpx;
    }

    .bor-btm {
      display: flex;
      border-bottom: 1px solid #d5d7e3;
    }

    .photo {
      display: flex;
      justify-content: space-between;

      image {
        width: 214rpx;
        height: 214rpx;
      }
    }

    .text {
      padding-bottom: 20rpx;
      padding-top: 4rpx;
      color: #333;

      text {
        color: #666;
      }
    }
  }
}

.hidden-title {
  background: #f5f5f5;
  padding: 20rpx;
  color: #999;
}

.bor-top {
  border-top: 20rpx solid #f5f5f5;
}

.blue {
  color: #88b4c4;
}

.icon-style {
  width: 52rpx;
  height: 42rpx;
  padding-right: 10rpx;
}
.input {
  display: inline-block;
  width: 100%;
  background-color: #f5f5f5;
  padding: 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  height: 60rpx;
}
.acceptance {
  margin-bottom: 10rpx;
}
.acceptance-list {
  display: flex;

  .acceptance-item {
    display: flex;
    align-items: center;
    margin-left: 20rpx;
    color: #333;

    .icon-style {
      width: 50rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .content {
      flex: 1;
    }
  }
}
</style>
